package de.hdm.articastle;

import java.util.List;

import android.graphics.Color;
import de.hdm.articastle.framework.Game;
import de.hdm.articastle.framework.Graphics;
import de.hdm.articastle.framework.Input.TouchEvent;
import de.hdm.articastle.framework.Screen;

public class GameScreen extends Screen {
	enum GameState {
		Ready, 
		Running, 
		Paused,
		GameOver
	}
	
	GameState state = GameState.Running;
	World world;
	
	public GameScreen(Game game) {
		super(game);
		world = new World();
	}
	
	private void drawWorld(World world) {
		Graphics g = game.getGraphics();
		
		g.clear(Color.WHITE);
		
		if (world.bullet != null && world.bullet.time != -1)
		{
			if (world.bullet.y > World.WORLD_HEIGHT)
				g.drawPixmap(Assets.arrow, world.bullet.x - 10, 15);
			else
				g.drawPixmap(Assets.bullet, world.bullet.x - (world.bullet.size / 2), 
						World.WORLD_HEIGHT - world.bullet.y - 32 - (world.bullet.size / 2));	
		}
		
		if (world.aimpoint != null)
		{
			if (world.currentPlayer == 1)
				g.drawLine(world.castle1.x + 21, 
						(World.WORLD_HEIGHT - world.castle1.y - 48), 
						world.aimpoint.x, 
						world.aimpoint.y, Color.BLACK);
			else
				g.drawLine(world.castle2.x + 21, 
						(World.WORLD_HEIGHT - world.castle2.y - 48), 
						world.aimpoint.x, 
						world.aimpoint.y, Color.BLACK);
		}
		
		g.drawPixmap(Assets.castle, world.castle1.x, 
				(World.WORLD_HEIGHT - world.castle1.y - 42), 
				(world.castle1.damage % 3) * 42,
				(world.castle1.damage / 3) * 42,
				42, 42);
		
		g.drawPixmap(Assets.castle, world.castle2.x, 
				(World.WORLD_HEIGHT - world.castle2.y - 42), 
				(world.castle2.damage % 3) * 42,
				(world.castle2.damage / 3) * 42,
				42, 42);
		
		for (int i = 0; i < world.landscape.length; i++)
		{
			g.drawLine(i, World.WORLD_HEIGHT, i, World.WORLD_HEIGHT - world.landscape[i], Color.GREEN);
		}
	}
	
	@Override
	public void update(float deltaTime) {
		world.update(deltaTime);
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		if (state == GameState.Running)
			updateRunning(touchEvents, deltaTime);
		if (state != GameState.GameOver)
		{
			if (world.castle1.damage == 5 || world.castle2.damage == 5)
				game.setScreen(new MainMenuScreen(game));
		}	
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		drawWorld(world);
	}

	@Override
	public void pause() {
		if (state == GameState.Running)
			state = GameState.Paused;
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
		if (world.bullet == null) 
		{
			int len = touchEvents.size();
			for (int i = 0; i < len; i++) {
				TouchEvent event = touchEvents.get(i);
				if (event.type == TouchEvent.TOUCH_DOWN) {
					world.aimpoint = new AimPoint(event.x, event.y);
				}
				else if (event.type == TouchEvent.TOUCH_DRAGGED) {
					if (world.aimpoint != null)
					{
						world.aimpoint.x = event.x;
						world.aimpoint.y = event.y;
					}
				}
				else if (event.type == TouchEvent.TOUCH_UP) {
					if (world.currentPlayer == 1)
						world.bullet = new Bullet((world.castle1.x + 21),
								world.castle1.y + 48, 
								32,
								(event.x - (world.castle1.x + 21)),
								(World.WORLD_HEIGHT - event.y - world.castle1.y));
					else
						world.bullet = new Bullet((world.castle2.x + 21),
								world.castle2.y + 48, 
								32,
								(event.x - (world.castle2.x + 21)),
								(World.WORLD_HEIGHT - event.y - world.castle2.y));
					
					world.calculateEndPoints();
					world.currentPlayer = ++world.currentPlayer % 2;
					world.aimpoint = null;					
				}
			}
		}
	}
}
